<cfparam name="Scholarship_ID" default="">
<cfparam name="Organization_ID" default="">
<cfparam name="Region_ID" default="">
<cfset view = "certificate">
<cftry>
	<cfscript>
		scholarshipGateway = new assets.calgrants.com.gateway.ScholarshipGateway();
		scholarships = scholarshipGateway.query(Scholarship_ID = IIf(IsNumeric(Scholarship_ID), Scholarship_ID, ""), organizationIDs = IIf(IsNumeric(Organization_ID), Organization_ID, ""), regionIDs = IIf(IsNumeric(Region_ID), Region_ID, ""), Cohort = 6);
		certTempl = "2014_Cash_For_College_Scholarship_Certificate_Form_v2.pdf";
		thisDir = GetDirectoryFromPath(ExpandPath("*.*"));
		if (scholarships.RecordCount eq 1) {
			certDir = thisDir;
		} else if (scholarships.RecordCount gt 1) {
			certDir = thisDir & "\" & "Cash_for_College_Scholarship_Certificates" & "_" & DateFormat(Now(), "yyyyddmm") & TimeFormat(Now(), "HHmmss");
		}
		//zipFile = "Cash_for_College_Scholarship_Certificates.zip";
	</cfscript>
	<!---
	REReplace(val, "[^0-9a-zA-Z]", "", "all")
	--->
	<cfif scholarships.RecordCount eq 1>
		<!--- single --->
		<cfset firstName = Trim(scholarships.First_Name)>
		<cfset lastName = Trim(scholarships.Last_Name)>
		<cfset fullName = firstName & " " & lastName />
		<cfset cert = certDir & "Cash_for_College_Scholarship_Certificate_" & scholarships.Claim_ID & "_" & REReplace(scholarships.First_Name, '[^0-9a-zA-Z]', '', 'all') & "_" & REReplace(scholarships.Last_Name, '[^0-9a-zA-Z]', '', 'all') & ".pdf">
		<cfpdfform action="populate" source="#certTempl#" name="popCert">
			<cfpdfformparam name="firstName" value="#firstName#">
			<cfpdfformparam name="lastName" value="#lastName#">
		</cfpdfform>
		<cfpdf action="write" flatten="yes" source="popCert" destination="#cert#" overwrite="yes">
		<cfset dlFile = GetFileFromPath(cert)>
	<cfelseif scholarships.RecordCount gt 1>
		<!--- multip zip --->
		<cfset zip = certDir & ".zip">
		<cfdirectory action="create" directory="#certDir#">
		<cfloop query="scholarships">
			<cfset firstName = Trim(First_Name)>
			<cfset lastName = Trim(Last_Name)>
			<cfset fullName = firstName & " " & lastName />
			<cfset cert = certDir & "\" & "Cash_for_College_Scholarship_Certificate_" & Claim_ID & "_" & REReplace(First_Name, '[^0-9a-zA-Z]', '', 'all') & "_" & REReplace(Last_Name, '[^0-9a-zA-Z]', '', 'all') & ".pdf">
			<cfpdfform action="populate" source="#certTempl#" name="popCert">
				<cfpdfformparam name="firstName" value="#firstName#">
				<cfpdfformparam name="lastName" value="#lastName#">
			</cfpdfform>
			<cfpdf action="write" flatten="yes" source="popCert" destination="#cert#" overwrite="yes">
		</cfloop>
		<cfzip file="#zip#" source="#certDir#">
		<cfset dlFile = GetFileFromPath(zip)>
	<cfelse>
		<p>Invalid Scholarship</p>
		<cfabort>
		<cfset view = "invalidScholarship" />
	</cfif>
	<cfcatch type="any">
		<!--- bury the error --->
		<cfdump var="#cfcatch#">
		<cfabort>
		<cfset view = "error" />
	</cfcatch>
</cftry>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>Cash for College Scholarship Certificate</title>
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="description" content="Cash for College Scholars">
	<meta name="author" content="Syntric">
	<link href="/assets/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
	<script src="/assets/jquery/jquery.min.js" type="text/javascript"></script>
	<script src="/assets/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
	<script src="/assets/calgrants/ui/common.js" type="text/javascript"></script>
</head>

<body style="padding-top: 60px;">
	<cfoutput>
		<div class="container">
			<div class="row">
				<div class="span12">
					<div class="hero-unit" style="background-color: white; border: solid 1px silver;">
						<h1 style="letter-spacing: normal; font-size: 2.4em;">Cash for College Scholarship Certificate(s)</h1>
						<cfswitch expression="#view#">
							<cfcase value="certificate">
								<p>The scholarship certificate(s) have been generated...</p>
								<p><a href="#dlFile#" target="_blank" class="btn btn-large btn-primary">Download</a></p>
							</cfcase>
							<cfdefaultcase>
								<p>Scholarship was not found.</p>
							</cfdefaultcase>
						</cfswitch>
					</div>
				</div>
			</div>
		</div>
	</cfoutput>
</body>
</html>